package com.xiaolun;

/**
 * @Author 硕鼠
 * @Date 2020/5/8 10:09
 * @Version 1.0
 * @Description 测试stop
 */
public class TestStop implements Runnable{

    //1.设置一个标志位
    private boolean flag = true;

    @Override
    public void run() {
        int i = 0;
        //2.线程中使用该标识
        while(flag){
            System.out.println("run...Thread"+(i++));
        }
    }

    //3.对外提供方法，改变标识
    public void stop(){
        this.flag = false;
    }

    public static void main(String[] args) {
        TestStop testStop = new TestStop();
        new Thread(testStop).start();

        for (int i = 0; i < 1000; i++) {
            System.out.println("main"+ i );
            if (i == 900){
                //4.调用stop方法，切换标志位，让线程停止
                testStop.stop();
                System.out.println("线程停止了");
            }
        }
    }
}
